<?php
class Trail extends CI_Controller{
	var $title=array(
			"race"=>"menu_trail_key_title",
			"title"=>"menu_trail_key_sub_title",
			"date"=>"menu_trail_key_date"
	);
	var $menu=array(
			"menu_key_home"=>"/nobressart/index.php/courses/trail/index",
			"menu_key_info"=>"/nobressart/index.php/courses/trail/info",
			"menu_key_access"	=>"/nobressart/index.php/courses/trail/acces",
			"menu_trail_key_pre_inscription"=>array(
					"menu_trail_key_inscription"=>"/nobressart/index.php/courses/trail/inscriptions",
					"menu_trail_key_inscription_list"=>"/nobressart/index.php/courses/trail/listeInscriptions"
			),
			"menu_key_pictures"	=>"/nobressart/index.php/courses/trail/photos",
			"menu_key_results"	=>"/nobressart/index.php/courses/trail/resultats",
			"menu_key_village"=>"/nobressart/index.php/courses/trail/village",
			"menu_key_links"=>"/nobressart/index.php/courses/trail/links",
			"menu_key_contact"=>"/nobressart/index.php/courses/trail/contact"

	);
	
	// -------------------------------- COMMON VARS
	public function getMenu(){
		return $this->menu;
	
	}
	public function getTitle(){
		return $this->title;
	}
	
	public function followLink($body){
		$this->render(array(),$body);
	}
	// ---------------------------------------------------------------------------- LINKS
	
	

	public function index(){
		$this->followLink("courses/trail/trail_view");
	}
	public function info(){
		$this->followLink("courses/trail/info_view");
	}

	public function organisation(){
		$this->followLink("courses/trail/organisation_view");
	}

	public function photos(){
		$this->followLink("courses/trail/photos_view");
	}
	public function resultats(){
		$this->followLink("courses/trail/resultats_view");
	}
	public function contact(){
		$this->followLink("commons/contact_view");
	}
	public function acces(){
		$this->followLink("commons/acces_view");
	}
	public function village(){
		$this->followLink("commons/village_view");
	}
	public function links(){
		$this->followLink("courses/trail/links_view");
	}
	//------------------------------------------------------------------------------ INSCRIPTIONS
	public function listeInscriptions(){

		$this->load->model('Inscription_trail_model', 'inscription');
		$data['inscriptions']=$this->inscription->getAll();
		$this->render($data,"courses/trail/listeInscriptions_view");
	}
	public function listeInscriptionsPrivate(){

		$this->load->model('Inscription_trail_model', 'inscription');
		$data['inscriptions']=$this->inscription->getAll();
		$this->render($data,"courses/trail/private/listeInscriptions_private_view");
	}
	public function inscriptions(){
		$this->load->model('Inscription_trail_model', 'inscription');
		$data['inscriptions']=$this->inscription->getAllLastRows(10);
		$this->render($data,"courses/trail/inscriptions_view");
	}
	
	public function inscriptionsPaye($id, $paye){
		$this->load->model('Inscription_trail_model', 'inscription');
		$data=array("paye"=>$paye);
		$this->db->update(Inscription_trail_model::tableName, $data, "id = ".$id);
		$this->listeInscriptionsPrivate();
	}
	public function submitInscriptions(){
		$this->load->library('form_validation');
		$this->load->model('Inscription_trail_model', 'inscription');
			

		$this->form_validation->set_error_delimiters('<li>', '</li>');
		$this->form_validation->set_rules('firstName', 'Nom', 'trim|strtoupper|required');
		$this->form_validation->set_rules('lastName', 'Prénom', 'trim|strtoupper|required');
		$this->form_validation->set_rules('course', 'Course', 'trim|required');
		$this->form_validation->set_rules('zipCode', 'Code postal', 'trim|required');
		$this->form_validation->set_rules('firstName', 'Inscription', "trim|strtoupper|callback_personUnique_check[".strtoupper(trim($this->input->post('lastName')."]")));
		$this->form_validation->set_rules('birthDate', 'Date de naissance', 'trim|required|callback_validDate_check');
		$this->form_validation->set_rules('email', 'Email', 'trim|required');

		if ($this->form_validation->run() == FALSE)		{
			$data['valid']=FALSE;
			$data['inscriptions']=$this->inscription->getAllLastRows(10);
			$this->render($data,"courses/trail/inscriptions_view");
		}else{

			$this->inscription->lastName=strtoupper(trim($this->input->post("lastName")));
			$this->inscription->firstName=strtoupper(trim($this->input->post("firstName")));
			$this->inscription->email=trim($this->input->post("email"));
			$this->inscription->sex=trim($this->input->post("sex"));
			$this->inscription->zipCode=trim($this->input->post("zipCode"));
			$this->inscription->country=trim($this->input->post("country"));
			$this->inscription->birthDate=$this->tools->getMysqlDate(trim($this->input->post("birthDate")));
			$this->inscription->club=trim($this->input->post("club"));
			$this->inscription->course=trim($this->input->post("course"));

			$this->inscription->saveOrUpdate();

			$data['title']="Trail de Nobressart (L'Elchertoise de jpp)";
			$data['date']="Samedi 15 Mars 9h30";

			$data['inscription']=$this->inscription;
			$data['valid']=TRUE;
			$data['inscriptions']=$this->inscription->getAllLastRows(10);
			$this->render($data,"courses/trail/inscriptions_view");
		}


	}
	public function validDate_check($str){
		$valid=false;
		$param=array("birthDate"=>$str);
		if (!$this->tools->valid_date($str))	{
			$this->form_validation->set_message('validDate_check', "La date doit posséder le format suivant: jj/mm/yyyy");
		}
		return true;
	}

	public function personUnique_check($firstName,$lastName){
		$valid=false;
		$param=array("firstName"=>$firstName,"lastName"=>$lastName);
		$this->db->select()->from(Inscription_trail_model::tableName)->where($param);
		$query=$this->db->get();
		$valid=$query->num_rows()===0;
		if (!$valid)	{
			$this->form_validation->set_message('personUnique_check', 'La personne est déja inscrite:'.$firstName." ".$lastName);
		}
		return $valid;
	}
	//------------------------------------------------------------------------------ COURSES

	public function miniTrailCourse(){
		$this->followLink("courses/trail/mini/course_view");
	}

	public function miniTrailDeniv(){
		$this->followLink("courses/trail/mini/deniv_view");
	}

	public function midiTrailCourse(){
		$this->followLink("courses/trail/midi/course_view");
	}
	public function midiTrailDeniv(){
		$this->followLink("courses/trail/midi/deniv_view");
	}
	public function maxiTrailCourse(){
		$this->followLink("courses/trail/maxi/course_view");
	}
	public function maxiTrailDeniv(){
		$this->followLink("courses/trail/maxi/deniv_view");
	}
	
}